package priv.mill.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import priv.mill.controller.util.RetenObj;
import priv.mill.entry.UserInfo;
import priv.mill.server.UserInfoService;

@Controller
@RequestMapping("UserInfo")
public class UserInfoController {

	@Autowired
	private UserInfoService userInfoService;

	private static final Logger logger = Logger.getLogger(UserInfoController.class);

	@RequestMapping("getUserInfoById")
	@ResponseBody
	public RetenObj getUserInfoById(@RequestParam("UserInfoId") Integer userInfoId) {
		try {
			return warpData(userInfoService.getUserInfoById(userInfoId), 200);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	private RetenObj warpData(Object object, int i) {
		RetenObj retenObj = new RetenObj();
		retenObj.setData(object);
		retenObj.setStatus(i);
		return retenObj;
	}

	@RequestMapping("getUserInfoByEntry")
	@ResponseBody
	public RetenObj getUserInfoByEntry(UserInfo userInfo) {
		try {
			return warpData(userInfoService.getUserInfoByEntry(userInfo), 200);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	@RequestMapping("getUserInfosByPage")
	@ResponseBody
	public RetenObj getUserInfosByPage(UserInfo userInfo) {
		try {
			return warpData(userInfoService.getUserInfosByPage(userInfo), 200);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	@RequestMapping("updateUserInfo")
	@ResponseBody
	public RetenObj updateUserInfo(UserInfo userInfo) {
		try {
			int result = userInfoService.updateUserInfo(userInfo);
			if (result > 0)
				return warpData(result, 200);
			else
				return warpData(result, 300);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}
	

	@RequestMapping("insertUserInfo")
	@ResponseBody
	public RetenObj insertUserInfo(UserInfo userInfo) {
		try {
			int result = userInfoService.insertUserInfo(userInfo);
			if (result > 0)
				return warpData(userInfo.getUId(), 200);
			else
				return warpData(result, 300);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	@RequestMapping("deleteUserInfo")
	
	@ResponseBody
	public RetenObj deleteUserInfo(Integer userInfoId) {
		try {
			int result = userInfoService.deleteUserInfo(userInfoId);
			if (result > 0)
				return warpData(result, 200);
			else
				return warpData(result, 300);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}

	}
}
